package com.xindaoapp.happypet.shop.ui;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.ImageBean;
import com.xindaoapp.happypet.ui.BaseFragment;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.view.MyGridView;

/**
 * 爱宠服务的tab
 * 
 * @author Yangqing
 * 
 */
public class LoveServiceFragment extends BaseFragment {

    private PullToRefreshListView pullListView;
    private MyGridView vGridView;
    private final ArrayList<ImageBean> topInfos = new ArrayList<ImageBean>();
    private TopIconAdapter topIconAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        // startActivity(new Intent(getActivity(), ShopActivity.class));
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_loveservice, null);
    }

    @Override
    protected void initViews() {
        pullListView = (PullToRefreshListView) getView().findViewById(R.id.love_listview);
        // 初始化头布局
        View headerView = View.inflate(mContext, R.layout.loveservice_top, null);
        vGridView = (MyGridView) headerView.findViewById(R.id.gv_topIcon);
        initTopData();
        topIconAdapter = new TopIconAdapter();
        vGridView.setAdapter(topIconAdapter);
        pullListView.setMode(Mode.PULL_FROM_START);
        if (pullListView.getRefreshableView().getHeaderViewsCount() <= 1) {
            pullListView.getRefreshableView().addHeaderView(headerView);
        }
        ListAdapter listAdapter = new ListAdapter();
        pullListView.setAdapter(listAdapter);
    }

    /**
     * 初始化top数据
     */
    private void initTopData() {

        ImageBean ib = new ImageBean(R.drawable.xc584, "商城");
        topInfos.add(ib);
        ImageBean ib2 = new ImageBean(R.drawable.xc584, "疫苗");
        topInfos.add(ib2);
        ImageBean ib3 = new ImageBean(R.drawable.xc584, "寄养");
        topInfos.add(ib3);
    }

    @Override
    protected void initEvents() {

        pullListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        vGridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:// 跳转商城
                        // startActivity(new Intent(mContext, ShopActivity.class));
                        break;
                    case 1:

                        break;
                }
            }
        });

    }


    @Override
    protected void loadDatas() {
        onDataArrived(true);
        pullListView.onRefreshComplete();
    }

    public class TopIconAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return topInfos.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHodler holder;
            if (convertView == null) {
                holder = new ViewHodler();
                convertView = View.inflate(mContext, R.layout.left_nemu_list, null);
                holder.iv_item_list = (CircleImageView) convertView.findViewById(R.id.iv_item_list);
                holder.tv_item_list = (TextView) convertView.findViewById(R.id.tv_item_list);
                convertView.setTag(holder);
            } else {
                holder = (ViewHodler) convertView.getTag();
            }
            ImageBean imageBean = topInfos.get(position);

            holder.iv_item_list.setBackgroundResource(imageBean.getIcon());
            holder.tv_item_list.setText(imageBean.getName());

            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return topInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public class ViewHodler {
            CircleImageView iv_item_list;
            TextView tv_item_list;
        }
    }

    public class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }



    }


}
